﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Manager_News_AddNews : System.Web.UI.Page
{
    string Fname;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        if (!IsPostBack)
        {
            BindTopic();
            BindData();
        }
    }

    private void BindTopic()
    {
        ListItem item = new ListItem();
        item.Text = "Tin tức";
        item.Value = "N";
        ddlChuDe.Items.Add(item);
        item = new ListItem();
        item.Text = "Khuyến mãi";
        item.Value = "S";
        ddlChuDe.Items.Add(item);
        item = new ListItem();
        item.Text = "Giới thiệu HD";
        item.Value = "I";
        ddlChuDe.Items.Add(item);
        //ddlChuDe.SelectedIndex = -1;
        if (Request.QueryString["KindNo"] != "" && Request.QueryString["KindNo"] != null)
            ddlChuDe.Items.FindByValue(Request.QueryString["KindNo"]).Selected = true;
    }

    private void BindData()
    {
        if (Request.QueryString["NewsID"] != "" && Request.QueryString["NewsID"] != null)
        {
            clsWebShop cls = new clsWebShop();

            PccMsg msg = new PccMsg();
            msg.CreateFirstNode("NewsID", Request.QueryString["NewsID"]);     
            msg.CreateFirstNode("Title", "");
            msg.CreateFirstNode("KindNo", "");
            msg.CreateFirstNode("Status", "");
            msg.CreateFirstNode("ViewCount", "20");
            DataTable dt = cls.Get_NewsByCondition(msg.GetXmlStr.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {
                txtTheLoai.Text = dt.Rows[0]["Title"].ToString();                                
                string sURL = dt.Rows[0]["imgURL"].ToString();
                if (sURL != "" && sURL != null)
                {
                    imgShowImage.Visible = true;
                    imgShowImage.Style.Add("cursor", "pointer");
                    imgURL.Src = "../../" + sURL;
                    hImg.Value = sURL;
                }
                ddlChuDe.SelectedItem.Selected = false;
                ddlChuDe.Items.FindByValue(dt.Rows[0]["KindNo"].ToString()).Selected = true;
                FCKeditor1.Value = dt.Rows[0]["Content"].ToString();

            }
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (FileU_HinhMinhHoa.HasFile)
        {
            System.Text.RegularExpressions.Regex objRegex = new System.Text.RegularExpressions.Regex(@"(.*?)\.jpg|(.*?)\.png|(.*?)\.gif|(.*?)\.bmp");
            if (!objRegex.IsMatch(FileU_HinhMinhHoa.FileName.ToLower()))
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Xin vui lòng chọn file hình ảnh')</script>");
                return;
            }
            else
            {
                System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("~") + "/Images/News/" + hImg.Value);
                if (file.Exists)
                    file.Delete();


                string FPath, strdate;
                strdate = System.DateTime.Today.ToString("yyyyMMddhhmmss");
                Fname = "Images/News/" + strdate + FileU_HinhMinhHoa.FileName;
                FPath = Server.MapPath("~/") + Fname;
                FileU_HinhMinhHoa.SaveAs(FPath);

            }
        }
        else // Giu lai file cu
            Fname = hImg.Value;

        clsWebShop cls = new clsWebShop();



        string NewsID = "0";
        if (Request.QueryString["NewsID"] != "" && Request.QueryString["NewsID"] != null)
        {
            NewsID = Request.QueryString["NewsID"];
        }
        PccMsg msg = new PccMsg();

        msg.CreateFirstNode("NewsID", NewsID);
        msg.CreateFirstNode("Title", txtTheLoai.Text.Trim());    
        msg.CreateFirstNode("Content", FCKeditor1.Value.ToString());
        msg.CreateFirstNode("ImgURL", Fname);
        msg.CreateFirstNode("KindNo", ddlChuDe.SelectedValue);
        msg.CreateFirstNode("Status","N");
        msg.CreateFirstNode("UpdID", Session["AdminUserID"].ToString());

        cls.Ins_Upd_News(msg.GetXmlStr.ToString());

        ReturnPage();
        //ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script> alert('Dữ liệu đã cập nhật thành công');  window.location.href='Product.aspx'</script>");
    }

    private void ReturnPage()
    {
        Response.Redirect("News.aspx?CodeID="+Request.QueryString["CodeID"]+"&KindNo="+ddlChuDe.SelectedValue);
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ReturnPage();
    }
}
